<?php
namespace GK;
class Json
{
    protected static $_instance;
    protected $json_format=["status"=>"code","msg"=>"msg","data"=>"data"];
    protected $json_status=[0,1];
    protected $json_msg=["error","sucess"];
    public static function getInstance()
    {
        if (!is_null(self::$_instance))
        {
            return self::$_instance;
        }
        $instance=new self();
        self::$_instance=$instance;
        return $instance;
    }
    public function setup($json_setup_data=[])
    {
        if (!empty($json_setup_data))
        {
            $this->json_status=array_keys($json_setup_data);
            $this->json_msg=array_values($json_setup_data);
        }
        return $instance;
    }
    public function output($status,$msg,$data)
    {
        header('Content-type:text/json');
        if (is_integer($status))
        { 
            $status=$this->json_status[$status]; 
        }
        if (is_integer($msg)) 
        { 
            $msg=$this->json_msg[$msg]; 
        }
        $jsondata=[
                    $this->json_format["status"]=>$status,
                    $this->json_format["msg"]=>$msg,
                    $this->json_format["data"]=>$data];
        $result=json_encode($jsondata,\JSON_UNESCAPED_UNICODE); 
        if (!$result)
        {
            throw new \Exception(json_last_error);
        }
        else
        {
            return $result;
        }
    }
    public function error($msg,$data=[],$code=0)
    {
        return $this->output($code,$msg,$data);
    }
    public function normal($msg,$data=[],$code=1)
    {
        return $this->output($code,$msg,$data);
    }

}
